﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using WordDoc = Microsoft.Office.Interop.Word.Document;


namespace WordTemplate2.Pannel
{
    public partial class Pannel_1 : Form
    {
        public bool is_change_cover = false;
        public int  document_type = 0;
        public Pannel_1()
        {
            InitializeComponent();
        }

        private void Pannel_1_Load(object sender, EventArgs e)
        {

        }

         //取消
        private void button2_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            this.Close();
        }

        private bool CheckValid()
        {
            if (ThisDocument.skip_check_flag)
                return true;
            if (this.textBox5.Text == "")
                return false;

            return true;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            object move = Word.WdMovementType.wdMove;
            object line = Word.WdUnits.wdLine;
            object count = 3;
            object wholestory = Word.WdUnits.wdStory;
            object character = Word.WdUnits.wdCharacter;
            object extend = Word.WdMovementType.wdExtend;

            if (!CheckValid())
            {
                MessageBox.Show("请填充完整");
            }
            if (!is_change_cover)
            {
                //load template
                if (document_type == 0)
                {
                    Globals.ThisDocument.CopyDataFromTemplate("标准模板.doc");
                }
                else
                {
                    Globals.ThisDocument.CopyDataFromTemplate("标准规范.doc");
                }

                Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark1", "");
                Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark3", "");
                Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark5", "");
                Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark6", "");
                Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark7", "");
                Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark8", "");


                string header = comboBox2.Text + " " + textBox2.Text + "—" + textBox3.Text;
                Globals.ThisDocument.ReplaceBookmarkContentWithString("标准编号", header);

                ThisDocument.current_header = header;
                if (textBox4.Text != "")
                {
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("代替标准编号", "代替 " + textBox4.Text);
                }
                else
                {
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("代替标准编号", "");
                }

                Globals.ThisDocument.ReplaceBookmarkContentWithString("封面标准名称", textBox5.Text);
                Globals.ThisDocument.ReplaceBookmarkContentWithStringKeepBookmark("正文标准名称", textBox5.Text);
                Globals.ThisDocument.ReplaceBookmarkContentWithString("稿件", comboBox3.Text);

                if (comboBox3.Text != "上网稿")
                {
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("稿件类型", "(" + comboBox3.Text + ")");
                }
                else
                {
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("稿件类型", "");
                }

                {
                    if (textBox8.Text.Length == 1)
                    {
                        textBox8.Text = "0" + textBox8.Text;
                    }

                    if (textBox9.Text.Length == 1)
                    {
                        textBox9.Text = "0" + textBox9.Text;
                    }


                    string date = textBox7.Text + "－" + textBox8.Text + "－" + textBox9.Text + "  发布";
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("封面发布日期", date);
                }

                {

                    if (textBox11.Text.Length == 1)
                    {
                        textBox11.Text = "0" + textBox11.Text;
                    }

                    if (textBox10.Text.Length == 1)
                    {
                        textBox10.Text = "0" + textBox10.Text;
                    }

                    string date1 = textBox12.Text + "－" + textBox11.Text + "－" + textBox10.Text + "  实施";
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("封面实施日期", date1);
                }


                Globals.ThisDocument.ReplaceBookmarkContentWithString("批准单位", comboBox4.Text);

                Globals.ThisDocument.Application.Selection.EndKey(ref wholestory, ref move);
                Globals.ThisDocument.Application.Selection.MoveUp(ref line, ref count, ref move);
                Globals.ThisDocument.Application.Selection.Delete(ref character, ref count);

            }
            else
            {
                //load template
                object count_t = 1;
                Globals.ThisDocument.MoveBookmarkSelected("mbookmark0");
                Globals.ThisDocument.ReplaceBookmarkContentWithString("mbookmark0", "");
                Globals.ThisDocument.Application.Selection.TypeText(" ");
                Globals.ThisDocument.Application.Selection.MoveLeft(ref character, ref count_t, ref extend);
                object r_temp = Globals.ThisDocument.Application.Selection.Range;
                Globals.ThisDocument.Application.ActiveDocument.Bookmarks.Add("mbookmark0", ref r_temp);
            
                Globals.ThisDocument.ReplaceBookmarkContentWithFileBookmark("mbookmark0", "标准模板.doc");

                string header = comboBox2.Text + " " + textBox2.Text + "—" + textBox3.Text;
                Globals.ThisDocument.ReplaceBookmarkContentWithString("标准编号", header);
 
                ThisDocument.current_header = header;

                Globals.ThisDocument.ReplaceBookmarkContentWithString("代替标准编号", textBox4.Text);
                Globals.ThisDocument.ReplaceBookmarkContentWithString("封面标准名称", textBox5.Text);
                Globals.ThisDocument.ReplaceBookmarkContentWithStringKeepBookmark("正文标准名称", textBox5.Text);
                Globals.ThisDocument.ReplaceBookmarkContentWithString("稿件", comboBox3.Text);

                if (comboBox3.Text != "上网稿")
                {
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("稿件类型", "(" + comboBox3.Text + ")");
                }
                else
                {
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("稿件类型", "");
                }

                {
                    if (textBox8.Text.Length == 1)
                    {
                        textBox8.Text = "0" + textBox8.Text;
                    }

                    if (textBox9.Text.Length == 1)
                    {
                        textBox9.Text = "0" + textBox9.Text;
                    }


                    string date = textBox7.Text + "－" + textBox8.Text + "－" + textBox9.Text + "  发布";
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("封面发布日期", date);
                }

                {

                    if (textBox11.Text.Length == 1)
                    {
                        textBox11.Text = "0" + textBox11.Text;
                    }

                    if (textBox10.Text.Length == 1)
                    {
                        textBox10.Text = "0" + textBox10.Text;
                    }

                    string date1 = textBox12.Text + "－" + textBox11.Text + "－" + textBox10.Text + "  实施";
                    Globals.ThisDocument.ReplaceBookmarkContentWithString("封面实施日期", date1);
                }

                Globals.ThisDocument.ReplaceBookmarkContentWithString("批准单位", comboBox4.Text);
            }

            Globals.ThisDocument.ChangeHeader(ThisDocument.current_header);
            Globals.ThisDocument.ChangeLandScapeHeader(ThisDocument.current_header);
            Globals.ThisDocument.MoveBookmarkSelected("mbookmark0");
            Globals.ThisDocument.Application.ActiveDocument.ActiveWindow.View.Type = Word.WdViewType.wdPrintView;

            Globals.ThisDocument.Application.Selection.HomeKey(ref wholestory, ref move);
            
            
            this.Visible = false;
            this.Close();
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {
            bool flag = Globals.ThisDocument.IsNumeric(textBox8.Text);
            if (!flag)
            {
                textBox8.Text = "01";
                return;
            }
        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {
            bool flag = Globals.ThisDocument.IsNumeric(textBox9.Text);
            if (!flag)
            {
                textBox8.Text = "01";
                return;
            }
        }

        private void textBox11_TextChanged(object sender, EventArgs e)
        {
            bool flag = Globals.ThisDocument.IsNumeric(textBox11.Text);
            if (!flag)
            {
                textBox8.Text = "01";
                return;
            }
        }

        private void textBox10_TextChanged(object sender, EventArgs e)
        {
            bool flag = Globals.ThisDocument.IsNumeric(textBox10.Text);
            if (!flag)
            {
                textBox8.Text = "01";
                return;
            }
        }
    }
}
